from __future__ import division

from ..attributes import *
from ..attributes import _

from .alignment import AlignedObject


class Screen(AlignedObject):
    x_range =_ (0, 1)
    y_range =_ (0, 1)
    
    pixel_size = Attribute(initial = (None, None), initiable = False, writable = True)
    
    @property
    def is_initialized(self):
        return self.pixel_size != (None, None)
    
    @property
    def top(self):
        return self.y_range[0]
    
    @property
    def bottom(self):
        return self.y_range[1]
    
    @property
    def left(self):
        return self.x_range[0]
    
    @property
    def right(self):
        return self.x_range[1]
    
    @property
    def raw_width(self):
        return self.right - self.left
    
    @property
    def raw_height(self):
        return self.bottom - self.top

    @property
    def width(self):
        return abs(self.raw_width)
    
    @property
    def height(self):
        return abs(self.raw_height)
    
    @property
    def size(self):
        return (self.width, self.height)
    
    @property
    def pixel_width(self):
        return self.pixel_size[0]
    
    @property
    def pixel_height(self):
        return self.pixel_size[1]

    def x_units_from_pixels(self, x):
        return x / self.pixel_width * self.raw_width + self.x_range[0]

    def y_units_from_pixels(self, y):
        return y / self.pixel_height * self.raw_height + self.x_range[1]

    def raw_width_from_pixels(self, width):
        return width / self.pixel_width * self.raw_width

    def raw_height_from_pixels(self, height):
        return height / self.pixel_height * self.raw_height

    def width_from_pixels(self, width):
        return width / self.pixel_width * self.width

    def height_from_pixels(self, height):
        return height / self.pixel_height * self.height

    def raw_pixels_from_width(self, width):
        return round(width * self.pixel_width / self.raw_width)

    def raw_pixels_from_height(self, height):
        return round(height * self.pixel_height / self.raw_height)
